package src.com.doit.demo03;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

//计算字符串中元素的个数
//.containsKey()判断map中是否有
public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个字符串");
        String line = sc.next();

        //创建map
        Map<Character,Integer>map=new HashMap<>();
        //字符串转char数组
        char[] chars = line.toCharArray();
        for (char c:chars)
        {
            boolean b = map.containsKey(c);//判断map中是否有c
            if(b)
            {
                Integer count = map.get(c);//根据键获得值
                map.put(c,count+1);
            }
            else
                map.put(c,1);
        }
        System.out.println(map);
    }
}
